package com.youku.baseUI
{
   import flash.display.Sprite;
   import flash.net.NetConnection;
   import flash.net.NetStream;
   import flash.media.Video;
   import flash.events.NetStatusEvent;
   import com.youku.events.CommonPlayerEvent;
   import flash.media.SoundTransform;
   
   public class SimpleVideoPlayer extends Sprite
   {
      
      public function SimpleVideoPlayer() {
         super();
         this._nc = new NetConnection();
         this._nc.connect(null);
         this._ns = new NetStream(this._nc);
         this._ns.client = this;
         this._ns.bufferTime = 5;
         this._width = DEFAULT_WIDTH;
         this._height = DEFAULT_HEIGHT;
         this._video = new Video(this._width,this._height);
         this._videoLoaded = false;
         this._firstBufferFull = false;
         addChild(this._video);
      }
      
      public static const VIDEO_INIT:String = "VIDEO_INIT";
      
      public static const VIDEO_LOADING:String = "VIDEO_LOADING";
      
      public static const VIDEO_LOADED:String = "VIDEO_LOADED";
      
      public static const VIDEO_END:String = "VIDEO_END";
      
      public static const VIDEO_ERROR:String = "VIDEO_ERROR";
      
      protected static const DEFAULT_WIDTH:Number = 640;
      
      protected static const DEFAULT_HEIGHT:Number = 480;
      
      private var _nc:NetConnection;
      
      private var _ns:NetStream;
      
      private var _nsObj:Object;
      
      private var _video:Video;
      
      private var _videoLoaded:Boolean;
      
      private var _firstBufferFull:Boolean;
      
      private var _duration:Number = 100;
      
      private var _videoURL:String = "";
      
      private var _width:Number;
      
      private var _height:Number;
      
      private var _videoWidth:Number = 0;
      
      private var _videoHeight:Number = 0;
      
      private var _keepRatio:Boolean = true;
      
      private var _state:String = "VIDEO_INIT";
      
      public function set videoURL(param1:String) : void {
         this._videoURL = param1;
      }
      
      public function play() : void {
         if(this._videoURL == "")
         {
            this.onVideoNotFound();
            return;
         }
         this._state = SimpleVideoPlayer.VIDEO_LOADING;
         this._nsObj = new Object();
         this._ns.addEventListener(NetStatusEvent.NET_STATUS,this.onNetEvent);
         this._ns.client = this._nsObj;
         this._nsObj.onMetaData = function(param1:Object):void
         {
            onVideoMetaData(param1);
         };
         this._video.attachNetStream(this._ns);
         this._video.smoothing = true;
         this._ns.play(this._videoURL);
      }
      
      private function onVideoMetaData(param1:Object) : void {
         this._videoWidth = !param1.width?320:param1.width;
         this._videoHeight = !param1.height?240:param1.height;
         this.setSize(this._width,this._height,this._keepRatio);
      }
      
      private function onNetEvent(param1:NetStatusEvent) : void {
         switch(param1.info.code)
         {
            case "NetStream.Play.Stop":
               this.onEnd();
               break;
            case "NetStream.Play.Start":
               if(this._state == SimpleVideoPlayer.VIDEO_LOADING)
               {
                  this._videoLoaded = true;
                  this.onVideoLoaded();
               }
               break;
            case "NetStream.Buffer.Full":
               if(!this._firstBufferFull)
               {
                  this._firstBufferFull = true;
                  this.onVideoLoaded();
               }
               break;
            case "NetStream.Play.StreamNotFound":
               this.onVideoNotFound();
               break;
         }
      }
      
      private function onVideoNotFound() : void {
         this._state = SimpleVideoPlayer.VIDEO_ERROR;
         dispatchEvent(new CommonPlayerEvent(CommonPlayerEvent.SVP_VIDEO_ERROR));
      }
      
      private function onEnd() : void {
         this._state = SimpleVideoPlayer.VIDEO_END;
         dispatchEvent(new CommonPlayerEvent(CommonPlayerEvent.SVP_VIDEO_END));
      }
      
      private function onVideoLoaded() : void {
         if((this._videoLoaded) && (this._firstBufferFull))
         {
            this._state = SimpleVideoPlayer.VIDEO_LOADED;
            dispatchEvent(new CommonPlayerEvent(CommonPlayerEvent.SVP_VIDEO_LOADED));
         }
      }
      
      public function onMetaData(param1:Object) : void {
         this._duration = param1.duration;
      }
      
      public function pause() : void {
         if(this._ns)
         {
            this._ns.pause();
         }
      }
      
      public function resume() : void {
         if(this._ns)
         {
            this._ns.resume();
         }
      }
      
      public function seek(param1:Number) : void {
         if(this._ns)
         {
            this._ns.seek(param1);
         }
      }
      
      public function stop() : void {
         try
         {
            this._nc.close();
         }
         catch(e:*)
         {
         }
         try
         {
            this._ns.close();
         }
         catch(e:*)
         {
         }
      }
      
      public function get position() : Number {
         if(this._ns)
         {
            return this._ns.time;
         }
         return 0;
      }
      
      public function setSize(param1:Number, param2:Number, param3:Boolean = true) : void {
         this._width = param1;
         this._height = param2;
         this._keepRatio = param3;
         if(param3 == false)
         {
            this._video.width = this._width;
            this._video.height = this._height;
            this._video.x = 0;
            this._video.y = 0;
            return;
         }
         if(this._videoWidth <= this._width && this._videoHeight <= this._height)
         {
            this._video.width = this._videoWidth;
            this._video.height = this._videoHeight;
         }
         else if(this._videoWidth > this._width || this._videoHeight > this._height)
         {
            if(this._width / this._height > this._videoWidth / this._videoHeight)
            {
               this._video.height = this._height;
               this._video.width = this._videoWidth * this._height / this._videoHeight;
            }
            else
            {
               this._video.width = this._width;
               this._video.height = this._videoHeight * this._width / this._videoWidth;
            }
         }
         
         this._video.x = (this._width - this._video.width) / 2;
         this._video.y = (this._height - this._video.height) / 2;
      }
      
      override public function get width() : Number {
         return this._width;
      }
      
      override public function get height() : Number {
         return this._height;
      }
      
      public function set volume(param1:Number) : void {
         if(param1 > 1)
         {
            param1 = 0;
         }
         if(param1 < 0)
         {
            param1 = 0;
         }
         var _loc2_:SoundTransform = this._ns.soundTransform;
         _loc2_.volume = param1;
         this._ns.soundTransform = _loc2_;
      }
   }
}
